package com.bana.fvs.app.main.page;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.BaseFragment;
import com.bana.fvs.app.main.fragment.EquipmentFragment;
import com.bana.fvs.app.main.fragment.FogZoneFragment;
import com.bana.fvs.app.main.fragment.MineFragment;
import com.bana.fvs.app.module.detaile.entity.CommonEntity;
import com.bana.fvs.app.module.login.page.LoginActivity;
import com.bana.fvs.app.mvp.contract.MainContract;
import com.bana.fvs.app.mvp.presenter.MainPresenter;
import com.bana.fvs.app.util.DateUtils;
import com.bana.fvs.app.util.DialogManager;
import com.bana.fvs.app.util.UserInfoUtils;
import com.wei.commonlibrary.app.AppManager;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.GlobalContext;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/7
 * Time   : 09:30
 * Desc   : 主页面(内含TAB页面)
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.IMainView {
    private static final String TAG = "dah_MainActivity";
    @BindView(R.id.tab1)
    RadioButton mTab1;
    @BindView(R.id.tab2)
    RadioButton mTab2;
    @BindView(R.id.tab3)
    RadioButton mTab3;
    @BindView(R.id.radioGroup)
    RadioGroup mRadioGroup;

    private BaseFragment currentFragment;
    private FogZoneFragment mFogZoneFragment;
    private EquipmentFragment mEquipmentFragment;
    private MineFragment mMineFragment;

    private FragmentManager fm;
    private FragmentTransaction transaction;

    //返回键控制
    private long startBackTime = 0;

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setTitleText("");
        mBaseTitleBarView.setLeftDrawable(-1);
        mBaseTitleBarView.setLeftGone();
    }

    @Override
    public void initWidget() {
        //绑定TAB页
        fm = getSupportFragmentManager();
        chooseFirst();
        replaceFragment(mFogZoneFragment, "mFogZoneFragment", getResources().getString(R.string.fogzonge));

        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            Resources resources = getResources();
            if (checkedId == mTab1.getId()) {
                if (mFogZoneFragment == null) {
                    mFogZoneFragment = BaseFragment.getFragment(mContext, FogZoneFragment.class, null);
                }
                replaceFragment(mFogZoneFragment, "mFogZoneFragment", resources.getString(R.string.fogzonge));
            } else if (checkedId == mTab2.getId()) {
                if (mEquipmentFragment == null) {
                    mEquipmentFragment = BaseFragment.getFragment(mContext, EquipmentFragment.class, null);
                }
                replaceFragment(mEquipmentFragment, "mEquipmentFragment", resources.getString(R.string.equipment));
            } else if (checkedId == mTab3.getId()) {
                if (mMineFragment == null) {
                    mMineFragment = BaseFragment.getFragment(mContext, MineFragment.class, null);
                }
                replaceFragment(mMineFragment, "mMineFragment", getResources().getString(R.string.mine));
            } else {

            }
        });
    }

    /**
     * 默认选中第一个TAB
     */
    private void chooseFirst() {
        //初始化第一个显示页面
        mFogZoneFragment = BaseFragment.getFragment(this, FogZoneFragment.class, null);
        //默认选中第一个TAB
        mTab1.setChecked(true);
    }

    /**
     * 切换Fragment
     *
     * @param fragment
     * @param tag
     * @param title
     */
    private void replaceFragment(BaseFragment fragment, String tag, String title) {
        if (currentFragment == fragment) return;
        mBaseTitleBarView.setTitleText(title);
        transaction = fm.beginTransaction();

        if (currentFragment == null) {//当前还没有添加fragment，管理器中是空的
            transaction.add(R.id.main_fl_content, fragment, tag);
        } else {
            //fragment是否已经提交到事务管理器中，已提交直接显示，反之则先添加，在显示、提交
            if (fragment.isAdded()) {
                transaction.hide(currentFragment).show(fragment);
            } else {
                transaction.hide(currentFragment).add(R.id.main_fl_content, fragment);
            }
        }
        transaction.commitAllowingStateLoss();
        currentFragment = fragment;
    }


    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.validate(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //currentFragment.resume();
        //mPresenter.validate(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        long timeMillis = System.currentTimeMillis();
        if (startBackTime != 0 && timeMillis - startBackTime < 2000) {
            AppManager.getAppManager().AppExit(GlobalContext.context, false);
            super.onBackPressed();
        } else {
            startBackTime = timeMillis;
            ToastUtil.showShort(GlobalContext.context, "再按一次退出");
        }
    }

    @Override
    public void onError(CommException e) {
        currentFragment.resume();
    }

    @Override
    public void onResponse(CommonEntity entity) {


    }

    @Override
    public void onTokenRefresh(CommonEntity entity) {
        currentFragment.resume();

        if(entity.getCzjg()==1){
            UserInfoUtils.getInstance().clearCache(mContext);//清空缓存数据
            //退出登录今天登录页面
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            AppManager.getAppManager().finishOtherActivity(LoginActivity.class);
            finish();
        }

    }
}
